﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using ALMKickstarter.Data;
using ALMKickstarter.Models;

namespace ALMKickstarter.Infrastructure
{
	public class FeatureRepository
	{
		ALMKickstarterEntities context;

		public FeatureRepository()
		{
			this.context = new ALMKickstarterEntities();
		}

		public List<FeatureModel> GetFeatures()
		{
			List<FeatureModel> features = new List<FeatureModel>();
			foreach (var f in context.Features)
				features.Add(Mapper.Map<FeatureModel>(f));
			return features;
		}

		public List<FeatureModel> GetFeaturesByProject(int id)
		{
			List<FeatureModel> features = new List<FeatureModel>();
			foreach (var f in context.Features.Where(o => o.ProjectId == id))
				features.Add(Mapper.Map<FeatureModel>(f));
			return features;
		}

		public FeatureModel GetFeatureById(int id)
		{
			return Mapper.Map<FeatureModel>(
				context.Features.Single(s => s.FeatureId == id));
		}

		public void InsertFeature(FeatureModel feature)
		{
			context.Features.Add(new Feature(feature.ProjectId, feature.Name));
			context.SaveChanges();
		}

		public void UpdateFeature(FeatureModel feature)
		{
			Feature f = context.Features.Single(s => s.FeatureId == feature.FeatureId);
			f.ProjectId = feature.ProjectId;
			f.Name = feature.Name;
			context.SaveChanges();
		}

		public void DeleteFeature(int id)
		{
			context.Features.Remove(context.Features.Single(s => s.FeatureId == id));
			context.SaveChanges();
		}
	}
}